<template>
    <div class="container" v-if="store.isSelected">
        <div class="add">
            <h3> 选择查询 </h3>
        </div>
        <el-divider />
        <el-table ref="singleTableRef" :data="selectLists" highlight-current-row style="width: 100%" border>
            <el-table-column type="index" width="50" />
            <el-table-column property="id" label="ID" />
            <el-table-column property="name" label="事件名" />
            <el-table-column property="area" label="肇事地区" />
            <el-table-column property="driver" label="驾驶人" />
            <el-table-column property="car_num" label="车牌号" />
        </el-table>
        <el-divider />
        <el-button type="danger" class="closePage" @click="closePage">x</el-button>
    </div>
</template>

<script setup>
import { mapStore } from '@/store';
import { toolMap } from '@/hooks/toolHook';
import { inject, onMounted, ref, watch } from 'vue';
// const { selectLists } = toolMap()
const store = mapStore()
const selectLists = ref([])
const closePage = () => {
    store.isSelected = false
    selectLists.value = []
}

onMounted(() => {
    const map = inject('$map')
    var Draw = new MapboxDraw({
        displayControlsDefault: false,
        controls: {
            point: false,
            line_string: false,
            polygon: true,
            trash: true
        }
    });
    map.addControl(Draw, 'bottom-left');
    map.on('draw.create', evt => {

        store.isSelected = true
        var features = Draw.getAll();
        // 获取到坐标
        var position = features.features[0].geometry.coordinates
        // 转换为turf -- polygon对象 判断要素在不在polygon对象中
        let polygon = turf.polygon(position)
        // 获取事件event的feature
        let layers = map.getStyle().layers
        let layers_id = layers.filter(item => item.id.startsWith('event'))
        layers_id.forEach(layer => {
            let features = map.queryRenderedFeatures({ layers: [layer.id] });
            features.forEach(feature => {
                // 获取事件坐标点
                let event_position = feature.geometry.coordinates
                // 转换为turf -- point对象
                let point = turf.point(event_position)
                var result = turf.booleanContains(polygon, point)
                if (result) {
                    selectLists.value.push(feature.properties)
                    console.log(selectLists.value)
                }
            })
        })
        Draw.delete(features.features[0].id);
    });
})

</script>

<style scoped lang="scss">
.container {
    position: fixed;
    top: 20%;
    left: 30%;
    z-index: 1000;
    border: solid 12px #99999979;
    border-radius: 10px;
    background: #fff;
    width: 600px;
    height: 500px;

    .add {
        height: 50px;
        background: rgba(135, 207, 235, 0.667);

        h3 {
            color: #fff;
            text-align: center;
            line-height: 50px;
        }
    }

}

.closePage {
    position: absolute;
    top: 0px;
    right: 0px;
}
</style>
